/*
 * downloader.cpp
 *
 *  Created on: Mar 24, 2014
 *      Author: alexander
 */

#include "Downloader.h"

#include "Poco/StreamCopier.h"
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/String.h"

using Poco::StreamCopier;
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;

const std::string Downloader::HTTP_PREFIX = "http://";
const std::string Downloader::HTTPS_PREFIX = "https://";

Downloader::Downloader(std::string address, unsigned int port)
	: proxyAddress(address), proxyPort(port) {}

std::vector<std::string> Downloader::fetchData(std::string url) const
//void Downloader::fetchData(std::string url, std::vector<std::string>& fetchedData) const
{
	Downloader::RequestData requestData = Downloader::parseURL(url);

	HTTPClientSession s(requestData.hostAddress);
	s.setProxy(proxyAddress, proxyPort);
	HTTPRequest request(HTTPRequest::HTTP_GET, requestData.httpRequest);

	s.sendRequest(request);

	HTTPResponse response;
	std::istream& rs = s.receiveResponse(response);

	std::vector<std::string> data;
	std::string str;
	while (getline(rs, str))
	{
		Poco::trimInPlace(str);
		if (str.empty() == false)
		{
			data.push_back(str);
		}
//		data.push_back(str);
	}

	return data;
}


Downloader::RequestData Downloader::parseURL(std::string& url)
{
	url = Downloader::removeURLPrefix(url);
	std::size_t slash = url.find_first_of("/");
//	std::cout << url.substr(0, slash) << std::endl;
//	std::cout << url.substr(slash) << std::endl;
	return Downloader::RequestData(url.substr(0, slash), url.substr(slash));
}

std::string Downloader::removeURLPrefix(std::string& url)
{
	std::size_t found = url.find(HTTP_PREFIX);
	if(found != std::string::npos)
	{
		return url.substr(found + HTTP_PREFIX.size());
		//std::cout << "Ripped: " << url << std::endl;
	}
	found = url.find(HTTPS_PREFIX);
	if(found != std::string::npos)
	{
		return url.substr(found + HTTPS_PREFIX.size());
		//std::cout << "Ripped: " << url << std::endl;
	}
	return url;
}
